// createRouter：创建router实例对象
// createWebHistory：创建history模式的路由

import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import Register from '@/views/Register/index.vue'
import User from '@/views/User/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import HomeProblemList from '@/views/Home/HomeProblemList.vue'
import HomeAbout from '@/views/Home/HomeAbout.vue'
import Problem from '@/views/Problem/index.vue'
import Description from '@/views/Problem/components/Detail/components/Description.vue'
import Answer from '@/views/Problem/components/Detail/components/Answer.vue'
import Comment from '@/views/Problem/components/Detail/components/Comment.vue'
import SubmitRecord from '@/views/Problem/components/Detail/components/SubmitRecord.vue'
import StudentClassInfo from '@/views/StudentClass/ClassInfo.vue'
import Work from '@/views/StudentClass/Work.vue'
import Course from '@/views/StudentClass/Course.vue'
import TeacherClassInfo from '@/views/TeacherClass/ClassInfo.vue'
import WorkRelease from '@/views/TeacherClass/WorkRelease.vue'
import ClassManage from '@/views/TeacherClass/ClassManage.vue'
import DataAnalysis from '@/views/TeacherClass/DataAnalysis.vue'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // path和component对应关系的位置
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '',
          component: Home,
        },
        {
          path: 'problemlist',
          component: HomeProblemList,
        },
        {
          path: 'class',
          component: {
            render(h) {
              return h()
            }
          },
          beforeEnter: (to, from, next) => {
            const userRole = useUserStore().userInfo.role;
            console.log(userRole)
            if (userRole === 'student') {
              next('/studentclass');
            } else if (userRole === 'teacher') {
              next('/teacherclass');
            } else {
              next('/login'); // 或者重定向到登录页
            }
          },
        },
        {
          path: 'studentclass',
          redirect: () => {
            return { path: '/studentclass/classinfo' }
          },
          meta: {
            requiresStudent: true,
            layoutHeaderType: 'student',
          },
          children: [
            {
              path: 'classinfo',
              component: StudentClassInfo,
            },
            {
              path: 'work',
              component: Work,
            },
            {
              path: 'course',
              component: Course,
            }
          ]
        },
        {
          path: 'teacherclass',
          redirect: () => {
            return { path: '/teacherclass/classinfo' }
          },
          meta: {
            requiresTeacher: true,
            layoutHeaderType: 'teacher',
          },
          children: [
            {
              path: 'classinfo',
              component: TeacherClassInfo,
            },
            {
              path: 'workrelease',
              component: WorkRelease,
            },
            {
              path: 'classmanage',
              component: ClassManage,
            },
            {
              path: 'dataanalysis',
              component: DataAnalysis,
            }
          ]
        },
        {
          path: 'about',
          component: HomeAbout,
        },
      ]
    },
    {
      path: '/problem',
      redirect: '/problemlist',
    },
    {
      path: '/problem/:id',
      component: Problem,
      children: [
        {
          path: '',
          redirect: to => {
            return { path: `/problem/${to.params.id}/description` }
          },
        },
        {
          path: 'description',
          component: Description,
        },
        {
          path: 'answer',
          component: Answer,
        },
        {
          path: 'comment',
          component: Comment,
        },
        {
          path: 'submitrecord',
          component: SubmitRecord,
        },
      ]
    },
    {
      path: '/login',
      component: Login,
    },
    {
      path: '/register',
      component: Register,
    },
    {
      path: '/user',
      component: User,
    },
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userRole = useUserStore().userInfo.role;

  // 学生端权限检查
  const requiresStudent = to.matched.some(record => record.meta.requiresStudent); // 检查是否需要学生权限
  if (requiresStudent && userRole !== 'student') {
    next('/'); // 如果不是学生用户，重定向到首页或其他页面
    return; // 停止执行后续代码
  }

  // 教师端权限检查
  const requiresTeacher = to.matched.some(record => record.meta.requiresTeacher); // 检查是否需要教师权限
  if (requiresTeacher && userRole !== 'teacher') {
    next('/'); // 如果不是教师用户，重定向到首页或其他页面
    return; // 停止执行后续代码
  }

  // 如果不需要权限检查或者权限验证通过，则允许导航
  next();
});

export default router